package com.ifuncat.gulimall.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class ListValueStringConstraintValidator implements ConstraintValidator<ListValue, String> {
    private final Set<String> set = new HashSet<>();

    /**
     * 初始化Validator,获得String类型的指定值
     */
    @Override
    public void initialize(ListValue constraintAnnotation) {
        String[] values = constraintAnnotation.stringValues();
        if (null != values) {
            set.addAll(Arrays.asList(values));
        }
    }

    /**
     * 检验参数值是否符合指定值
     */
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
